package com.sj.controller;

import com.sj.common.dto.footprint.FootprintParam;
import com.sj.common.exception.CustomException;
import com.sj.common.lang.ApiResult;
import com.sj.entity.Footprint;
import com.sj.service.FootprintService;
import com.sj.utils.MineUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 足迹表 前端控制器
 * </p>
 *
 * @author 孙峻
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/footprint")
@Api(tags = "足迹模块")
public class FootprintController {

    @Autowired
    private FootprintService footprintService;

    @Autowired
    private MineUtils mineUtils;


    @PutMapping("/add")
    public ApiResult addFootPrint(@RequestBody FootprintParam footprintParam) {
        Integer uid = null;
        try {
            uid = mineUtils.getUidByToken();
        } catch (CustomException e) {
            return ApiResult.success("用户还未登陆，暂时不添加足迹");
        }

        // 首先判断是否有过浏览足迹记录
        Footprint footprint = footprintService.lambdaQuery().eq(Footprint::getArticleId, footprintParam.getArticleId()).eq(Footprint::getUserId, uid).one();
        if (footprint == null) {
            footprint = Footprint.builder().articleId(footprintParam.getArticleId()).userId(uid).build();
        }
        footprint.setUpdateTime(LocalDateTime.now());
        footprintService.saveOrUpdate(footprint);
        return ApiResult.success("添加足迹成功");
    }

}
